/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;
import DTO.CGiaBan;
import DTO.CFields;
import DTO.CSanPham;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author quangphuc
 */
public class CGiaBanDAO extends IData<CGiaBan>{
    public CGiaBanDAO(){
        this.query = "update " + CFields.tb_gia_ban +" set " 
                + CFields.gia_ban_khtt+ "=?," + CFields.gia_ban_khvl+ "=?,"
                + CFields.gia_nhap +"=?," + CFields.nguoi_sua + "=?," 
                + CFields.san_pham_id+"=?," + CFields.ngay_sua + "=? where "
                + CFields.id + "=?";
    }
 @Override
    protected void add(CGiaBan item, int t) {
         try {
            ps.setDouble(1, item.getGiaBanKHTT());
            ps.setDouble(2, item.getGiaBanKHVL());
            ps.setDouble(3, item.getGiaNhap());
            ps.setString(4, item.getNguoiSua());
            ps.setLong(5, item.getSanPhamId());
            ps.setTimestamp(6, item.getNgaySua());
            if(t == 1){
                ps.setLong(7, item.getId());
            }
            ps.addBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CGiaBanDAO.class.getName()).log(Level.SEVERE, null, ex);
             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }
    @Override
    public CGiaBan add(CGiaBan item) {
        try {
            ps.setDouble(1, item.getGiaBanKHTT());
            ps.setDouble(2, item.getGiaBanKHVL());
            ps.setDouble(3, item.getGiaBanVip());
            ps.setString(4, item.getNguoiTao());
            ps.setLong(5, item.getSanPhamId());
            ps.setTimestamp(6, item.getNgayTao());
            ps.setDouble(7, item.getGiaNhap());
            ps.addBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CGiaBanDAO.class.getName()).log(Level.SEVERE, null, ex);
             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }       
        return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int addrows(List<CGiaBan> list, SubConfig config) {
        String mysql = " insert into "+ CFields.tb_gia_ban + "("
                + CFields.gia_ban_khtt + "," + CFields.gia_ban_khvl + ","
                + CFields.gia_ban_kvip + "," + CFields.nguoi_tao + ","
                + CFields.san_pham_id + "," + CFields.ngay_tao + ","
                + CFields.gia_nhap
                +") values(?,?,?,?,?,?,?)";
        int rs = 0;
        try {
            ps = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
             for(CGiaBan item : list){
                this.add(item);
            }
           int []n = ps.executeBatch();
           rs = n.length;
        } catch (SQLException ex) {
            Logger.getLogger(CGiaBan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
    
    @Override
    public CGiaBan Find(CGiaBan item, List<CGiaBan> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(CGiaBan item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object insertOnSubmit(CGiaBan item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object updateOnSubmint(CGiaBan item, SubConfig config) {
         String mysql = "update " + CFields.tb_gia_ban +" set " 
                + CFields.gia_ban_khtt+ "=?," + CFields.gia_ban_khvl+ "=?,"
                + CFields.gia_ban_kvip+"=?," + CFields.nguoi_sua + "=?,"
                + CFields.san_pham_id+"=?," + CFields.ngay_sua + "=? where "
                + CFields.id + "=?";
         Object result = 0;
           try {
               PreparedStatement cs = config.connec.prepareStatement(mysql);
                cs.setDouble(1, item.getGiaBanKHTT());
                cs.setDouble(2, item.getGiaBanKHVL());
                cs.setDouble(3, item.getGiaBanVip());
                cs.setString(4, item.getNguoiTao());
                cs.setLong(5, item.getSanPhamId());
                cs.setTimestamp(6, item.getNgayTao());
                cs.setLong(7, item.getId());
                result = cs.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(CGiaBanDAO.class.getName()).log(Level.SEVERE, null, ex);
             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
           return result;
    }

    @Override
    public Object deleteOnSubmint(CGiaBan item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CGiaBan getItem(ResultSet rs) {
        CGiaBan item = new CGiaBan();
        try {
            //
            item.setGiaBanKHTT(rs.getDouble(CFields.gia_ban_khtt));
            item.setGiaBanKHVL(rs.getDouble(CFields.gia_ban_khvl));
            item.setGiaBanVip(rs.getDouble(CFields.gia_ban_kvip));
            item.setId(rs.getLong(CFields.id));
            item.setNgayTao(rs.getTimestamp(CFields.ngay_tao));
            item.setNgaySua(rs.getTimestamp(CFields.ngay_sua));
            item.setNguoiSua(rs.getString(CFields.nguoi_sua));
            item.setNguoiTao(rs.getString(CFields.nguoi_tao));
            item.setSanPhamId(rs.getLong(CFields.id));
            item.setTenSanPham(rs.getString(CFields.ten_san_pham));
            item.setGiaNhap(rs.getDouble(CFields.gia_nhap));
        } catch (SQLException ex) {
            Logger.getLogger(CGiaBanDAO.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        return item;
    }

    @Override
    public List<CGiaBan> getAll(SubConfig config) {
        this.sWhere = " where sp." + CFields.id + " = gb." + CFields.san_pham_id;
        String mysql ="select gb.*,sp." + CFields.ten_san_pham + " from " 
                + CFields.tb_gia_ban + " gb ," + CFields.tb_san_pham + " sp " + this.sWhere;
        return this.getAll(mysql, config);
    }   

  
}
